import redis

# base on redis-deque
deque_key_prefix = "url_list:"


class UrlManager:
    def __init__(self):
        redis_pool = redis.ConnectionPool(
            host='192.168.88.131',
            port=6379,
            password='123456',
            db=0,
            decode_responses=True)
        self.redis_conn = redis.Redis(connection_pool=redis_pool)

    def add_new_url(self, province, city_url):
        if city_url is None or len(city_url) == 0:
            print("请传递url的值。")
            return
        self.redis_conn.lpush(deque_key_prefix + province, city_url)
        print("%s 保存成功。" % city_url)

    def get_url(self, province):
        if self.has_url(province):
            this_url = self.redis_conn.rpop(deque_key_prefix + province)
            # print(this_url + "已获取")
            return this_url

    def has_url(self, province):
        return self.redis_conn.exists(deque_key_prefix + province) == 1


